package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.CouponManagementService;
import cc.rengu.igas.momp.facade.request.ClaimCouponRequest;
import cc.rengu.igas.momp.facade.request.QueryCouponDetailRequest;
import cc.rengu.igas.momp.facade.request.RedeemCouponRequest;
import cc.rengu.igas.momp.facade.request.ReturnCouponRequest;
import cc.rengu.igas.momp.facade.response.ClaimCouponResponse;
import cc.rengu.igas.momp.facade.response.QueryCouponDetailResponse;
import cc.rengu.igas.momp.facade.response.RedeemCouponResponse;
import cc.rengu.igas.momp.facade.response.ReturnCouponResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 卡券管理接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/24 11:50
 */
@SuppressWarnings("unchecked")
public class CouponManagementServiceImpl implements CouponManagementService {

    /**
     * 卡券领取Dubbo接口实现
     *
     * @param claimCouponRequest 领取卡券接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ClaimCouponResponse> 卡券领取接口应答对象
     * @author Jinan Liu
     * @date 2020/4/7 09:41
     */
    @Override
    public Result<ClaimCouponResponse> claimCoupon(ClaimCouponRequest claimCouponRequest) {

        DubboService<ClaimCouponRequest> dubboService = new DubboServiceImpl();
        Result<ClaimCouponResponse> claimCouponResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.CLAIM_COUPON.getTxnNum().equals(claimCouponRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ClaimCouponResponse> dubboResult = dubboService.callAtomService(claimCouponRequest,
                    MompTxnNumEnum.CLAIM_COUPON.getService(), ClaimCouponResponse.class);
            claimCouponResponseResult.setResult(dubboResult.getResult());
            claimCouponResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ClaimCouponResponse claimCouponResponse = new ClaimCouponResponse();
            claimCouponResponseResult.setResult(claimCouponResponse);
            claimCouponResponse.setHeader(claimCouponRequest.getHeader());
            claimCouponResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                claimCouponResponse.setRespCode(((BizException) e).getCode());
                claimCouponResponse.setRespDesc(e.getMessage());
            } else {
                claimCouponResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                claimCouponResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return claimCouponResponseResult;
    }

    /**
     * 卡券信息查询接口Dubbo接口实现
     *
     * @param queryCouponDetailRequest 卡券信息查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryCouponDetailResponse> 卡券信息查询接口应答对象
     * @author Jinan Liu
     * @date 2020/4/7 09:35
     */
    @Override
    public Result<QueryCouponDetailResponse> queryCouponDetail(QueryCouponDetailRequest queryCouponDetailRequest) {

        DubboService<QueryCouponDetailRequest> dubboService = new DubboServiceImpl();
        Result<QueryCouponDetailResponse> queryCouponDetailResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_COUPON_DETAIL.getTxnNum().equals(queryCouponDetailRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryCouponDetailResponse> dubboResult = dubboService.callAtomService(queryCouponDetailRequest,
                    MompTxnNumEnum.QUERY_COUPON_DETAIL.getService(), QueryCouponDetailResponse.class);
            queryCouponDetailResponseResult.setResult(dubboResult.getResult());
            queryCouponDetailResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryCouponDetailResponse queryCouponDetailResponse = new QueryCouponDetailResponse();
            queryCouponDetailResponseResult.setResult(queryCouponDetailResponse);
            queryCouponDetailResponse.setHeader(queryCouponDetailRequest.getHeader());
            queryCouponDetailResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryCouponDetailResponse.setRespCode(((BizException) e).getCode());
                queryCouponDetailResponse.setRespDesc(e.getMessage());
            } else {
                queryCouponDetailResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryCouponDetailResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return queryCouponDetailResponseResult;
    }

    /**
     * 卡券核销Dubbo接口实现
     *
     * @param redeemCouponRequest 卡券核销接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.RedeemCouponResponse> 卡券核销接口应答对象
     * @author Jinan Liu
     * @date 2020/4/7 09:42
     */
    @Override
    public Result<RedeemCouponResponse> redeemCoupon(RedeemCouponRequest redeemCouponRequest) {

        DubboService<RedeemCouponRequest> dubboService = new DubboServiceImpl();
        Result<RedeemCouponResponse> redeemCouponResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.REDEEM_COUPON.getTxnNum().equals(redeemCouponRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<RedeemCouponResponse> dubboResult = dubboService.callAtomService(redeemCouponRequest,
                    MompTxnNumEnum.REDEEM_COUPON.getService(), RedeemCouponResponse.class);
            redeemCouponResponseResult.setResult(dubboResult.getResult());
            redeemCouponResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            RedeemCouponResponse redeemCouponResponse = new RedeemCouponResponse();
            redeemCouponResponseResult.setResult(redeemCouponResponse);
            redeemCouponResponse.setHeader(redeemCouponRequest.getHeader());
            redeemCouponResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                redeemCouponResponse.setRespCode(((BizException) e).getCode());
                redeemCouponResponse.setRespDesc(e.getMessage());
            } else {
                redeemCouponResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                redeemCouponResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return redeemCouponResponseResult;
    }

    /**
     * 卡券退回Dubbo接口实现
     *
     * @param returnCouponRequest 卡券退回接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.ReturnCouponResponse> 卡券退回接口应答对象
     * @author Jinan Liu
     * @date 2020/4/7 09:42
     */
    @Override
    public Result<ReturnCouponResponse> returnCoupon(ReturnCouponRequest returnCouponRequest) {

        DubboService<ReturnCouponRequest> dubboService = new DubboServiceImpl();
        Result<ReturnCouponResponse> returnCouponResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.RETURN_COUPON.getTxnNum().equals(returnCouponRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ReturnCouponResponse> dubboResult = dubboService.callAtomService(returnCouponRequest,
                    MompTxnNumEnum.RETURN_COUPON.getService(), ReturnCouponResponse.class);
            returnCouponResponseResult.setResult(dubboResult.getResult());
            returnCouponResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ReturnCouponResponse returnCouponResponse = new ReturnCouponResponse();
            returnCouponResponseResult.setResult(returnCouponResponse);
            returnCouponResponse.setHeader(returnCouponRequest.getHeader());
            returnCouponResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                returnCouponResponse.setRespCode(((BizException) e).getCode());
                returnCouponResponse.setRespDesc(e.getMessage());
            } else {
                returnCouponResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                returnCouponResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return returnCouponResponseResult;
    }
}
